<?php

$html = '';
if (count($data['errors']) == 0) {
    $html .= '<div class="authorProfile"><h2><a href="index.php?p=author&author_id=' . $data['author']['author_id'] . '">' . htmlspecialchars($data['author']['author_name']) . '</a></h2></div>';
    if (count($data['books']) > 0) {
        $html .= '<div class="booksResults"><table><thead><tr><th class="thBooks">Книги</th><th class="thAuthors">Автори</th></tr></thead><tbody>';
        $oddEven = 'odd';
        foreach ($data['books'] as $book) {
            if ($oddEven == 'odd') {
                $oddEven = 'even';
            } else {
                $oddEven = 'odd';
            }
            $html .= '<tr class="' . $oddEven . '"><td><a href="index.php?p=book&book_id=' . $book['book_id'] . '">' . htmlspecialchars($book['book_title']) . '</a></td><td>';
            $authors = array();
            foreach ($book['authors'] as $author) {
                $authors[] = '<a href="index.php?p=author&author_id=' . $author['id'] . '">' . htmlspecialchars($author['name']) . '</a>';
            }
            $html .= implode(', ', $authors);
            $html .= '</td></tr>';
        }
        $html .= '</tbody></table></div>';
    } else {
        $html .= '<p class="error">Не са намерени книги за този автор</p>';
    }
} else {
    foreach ($data['errors'] as $error) {
        $html .= '<p class="error">' . $error . '</p>';
    }
}

echo $html;